package ase.eleitweg.common;

import java.io.*;
import java.util.*;

public class Group implements Serializable {
	public static final long serialVersionUID = 1L;
	
	private int id;
	
	private String name;
	private String description;
	
	private List<User> users;

	public Group(int id, String name, List<User> users) {
		super();
		this.id = id;
		this.name = name;
		this.users = users;
	}

	public Group(int id, String name) {
		super();
		this.id = id;
		this.name = name;
		this.users = new Vector<User>();
	}
	
	public Group(int id, String name, String description) {
		super();
		this.id = id;
		this.name = name;
		this.users = new Vector<User>();
		this.description = description;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * Searches for a user given an id
	 * @param id
	 * @return Returns the user with the given id if the such user, otherwise null.
	 */
	public User getUserById(int id) {
		for(User u : users) {
			if(u.getId() == id) {
				return u;
			}
		}
		return null;
	}
	
	public boolean addUser(User u) {
		for (User tmp : users) {
			if (tmp.getId() == u.getId()) {
				return false;
			}
		}
		users.add(u);
		return true;
	}
	
	public boolean isMember(User u) {
		for(User tmp : users) {
			if(u.getId() == tmp.getId() && u.getUsername().equals(tmp.getUsername())) {
				return true;
			}
		}
		return false;
	}

}
